
package com.kanjian.music.download;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import android.net.http.AndroidHttpClient;
import android.os.Build;

public class DownloadHelper {

    public static HttpClient mHttpClient;

    static {
        mHttpClient = AndroidHttpClient.newInstance("");
    }

    public static MusicDownloadEntity getInputStreamFromUrl(String url)
            throws MalformedURLException, IOException {
        MusicDownloadEntity entity = new MusicDownloadEntity();
        if (Build.VERSION.SDK_INT >= 9) {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            if (connection.getResponseCode() == 200) {
                String contentLengthStr = connection.getHeaderField("Content-Length");
                if (contentLengthStr != null && contentLengthStr.length() > 0) {
                    entity.mContentLength = Integer.parseInt(contentLengthStr);
                }
                entity.mInpustream = connection.getInputStream();
                return entity;
            }
        } else {
            HttpGet request = new HttpGet(url);
            HttpResponse response = mHttpClient.execute(request);
            if (response != null) {
                int statusLine = response.getStatusLine().getStatusCode();
                if (statusLine == 200) {
                    HttpEntity responseEntity = response.getEntity();
                    if (responseEntity != null) {
                        entity.mInpustream = responseEntity.getContent();
                        entity.mContentLength = (int) responseEntity.getContentLength();
                        return entity;
                    }
                }
            }
        }
        return null;
    }

    public static MusicDownloadEntity getInputStreamFromUrl(String url, long start)
            throws MalformedURLException, IOException {
        MusicDownloadEntity entity = new MusicDownloadEntity();
        if (Build.VERSION.SDK_INT >= 9) {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestProperty("Range", "bytes=" + start + "-");
            if (connection.getResponseCode() == 206) {
                entity.mContentLength = connection.getContentLength();
                entity.mInpustream = connection.getInputStream();
                return entity;
            }
        } else {
            HttpGet request = new HttpGet(url);
            request.addHeader("Range", "bytes=" + start + "-");
            HttpResponse response = mHttpClient.execute(request);
            if (response != null) {
                int statusLine = response.getStatusLine().getStatusCode();
                if (statusLine == 206) {
                    HttpEntity responseEntity = response.getEntity();
                    if (responseEntity != null) {
                        entity.mInpustream = responseEntity.getContent();
                        entity.mContentLength = (int) responseEntity.getContentLength();
                        return entity;
                    }
                }
            }
        }
        return null;
    }

}
